/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Order;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.enums.EOrderStatus;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.view.MyListView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/23.
 * desc    : 我的订单列表适配器
 */
public class OrderAdapter extends AbsAdapter<Order> {
    private int mRedColor;
    private int mGreenColor;
    private int mGrayColor;

    public OrderAdapter(Context context) {
        super(context);
        mRedColor = ContextCompat.getColor(context, R.color.res_color_primary);
        mGreenColor = ContextCompat.getColor(context, R.color.res_color_accent);
        mGrayColor = ContextCompat.getColor(context, R.color.res_color_black_secondary_text);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_my_order_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
            holder.mAdapter = new OrderProductAdapter(mContext);
            holder.mMlvList.setAdapter(holder.mAdapter);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Order data = getItem(position);
        //进入订单详情
        holder.mMlvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                OuerDispatcher.presentOrderDetailActivity(mContext, data.getId(), null);
            }
        });

        //数量
        int count = 0;
        for(Product product : data.getGoods()) {
            count += product.getCount();
        }
        holder.mTvNum.setText(mContext.getString(R.string.my_order_num, count));

        //状态
        switch (data.getStatus()) {
            case EOrderStatus.UNPAID:
                holder.mTvStatus.setText(R.string.my_order_status_unpaid);
                holder.mTvStatus.setTextColor(mRedColor);
                break;
            case EOrderStatus.SENDING:
                holder.mTvStatus.setText(R.string.my_order_status_sending);
                holder.mTvStatus.setTextColor(mRedColor);
                break;
            case EOrderStatus.FINISH:
                holder.mTvStatus.setText(R.string.my_order_status_finish);
                holder.mTvStatus.setTextColor(mGreenColor);
                break;
            case EOrderStatus.CANCEL:
                holder.mTvStatus.setText(R.string.my_order_status_cancel);
                holder.mTvStatus.setTextColor(mGrayColor);
                break;
            default:
                break;
        }

        //商品
        holder.mAdapter.setList(data.getGoods());

        //实付
        holder.mTvAmount.setText(Html.fromHtml(mContext.getString(R.string.my_order_amount, data.getAmount())));

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //数量
        @Bind(R.id.order_id_num)
        TextView mTvNum;
        //状态
        @Bind(R.id.order_id_status)
        TextView mTvStatus;
        //商品
        @Bind(R.id.order_id_list)
        MyListView mMlvList;
        //实付
        @Bind(R.id.order_id_amount)
        TextView mTvAmount;

        OrderProductAdapter mAdapter;
    }
}
